using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using buildingLib.Administration;

namespace _dmis.fixedasset
{
    public partial class FixedAssetOutDlg : _dmis.UIFormBase
    {
        FixedAssets m_assets;
        FixedAssetOutTicket m_outticket;

        public FixedAssetOutDlg()
        {
            InitializeComponent();
        }

        private void FixedAssetOutDlg_Load(object sender, EventArgs e)
        {
            m_assets = FixedAssets.GetInList();
            this.fixedAssetsBindingSource.DataSource = m_assets;
            m_outticket = FixedAssetOutTicket.NewTicket();
            //this.fixedAssetsOutBindingSource.DataSource = m_outticket.Outs;
            this.fixedAssetOutTicketBindingSource.DataSource = m_outticket;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            FixedAssert fa = this.fixedAssetsBindingSource.Current as FixedAssert;
            if (fa != null)
            {
                m_assets.Remove(fa);
                m_outticket.Outs.Add(fa);
            }
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            FixedAssert fa = this.outsBindingSource.Current as FixedAssert;
            if (fa != null)
            {
                m_outticket.Outs.Remove(fa);
                m_assets.Add(fa);
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.fixedAssetOutTicketBindingSource.RaiseListChangedEvents = false;
            fixedAssetOutTicketBindingSource.EndEdit();
            try
            {
                m_outticket.Save();
            }
            catch (Csla.DataPortalException ex)
            {
                MessageBox.Show(ex.BusinessException.ToString(),
                  "Error saving", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                  "Error Saving", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            finally
            {
                this.Close();
            }
        }
       
        private void fixedAssetOutTicketBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            this.btn_OK.Enabled = m_outticket.IsSavable;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }     
    }
}

